# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)

#设置为交叉编译 
set(CMAKE_SYSTEM_NAME "Generic")
 
 
#设置C标准
# set(CMAKE_C_STANDARD 11)
# set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#跳过检查 gcc是否可用
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_C_COMPILER_WORKS 1) 


#设置为debug编译
set(CMAKE_BUILD_TYPE "Debug")
 

 

# 设置GCC
set(CROSS_COMPILE "E:/armgcc/bin/arm-none-eabi-")
set(CMAKE_C_COMPILER  ${CROSS_COMPILE}gcc.exe)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++.exe)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}gcc.exe)

message("gcc:" ${CMAKE_C_COMPILER})

 
#设置工程名称
set(project_name_ nano)
project(${project_name_} C )
 
  
set(CMAKE_C_FLAGS "\
-mcpu=cortex-m3 \
-mthumb \
-mfloat-abi=soft \
-Wall \
-fdata-sections -ffunction-sections \
 -MMD -MP \
-g \
-gdwarf-2")
message("CMAKE_C_FLAGS:" ${CMAKE_C_FLAGS})

set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -MMD -MP")


#设置 链接flags
set(CMAKE_C_LINK_FLAGS "-mcpu=cortex-m3 \
-Wl,--gc-sections \
-T${CMAKE_CURRENT_SOURCE_DIR}/link.ld \
-Wl,-Map=${project_name_}.map,--cref")
message("CMAKE_C_LINK_FLAGS:" ${CMAKE_C_LINK_FLAGS})

#debug配置
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3")

set (CMAKE_EXECUTABLE_SUFFIX ".elf")

# 目标文件
set(MY_TARGET ${project_name_})

#工程文件和include目录设置 
include_directories(src .)
#添加.C和.S文件
add_executable(
  ${MY_TARGET}
  src/main.c src/led.c src/interrupt.c   src/led.c src/gcc_startup.c )


# 将elf文件转hex和bin
set(CMAKE_OBJCOPY "${CROSS_COMPILE}objcopy")
add_custom_command(TARGET ${MY_TARGET} POST_BUILD COMMAND ${CMAKE_OBJCOPY} -O ihex ${MY_TARGET}.elf ${project_name_}.hex)

# bin文件
add_custom_command(TARGET ${MY_TARGET} POST_BUILD COMMAND  ${CMAKE_OBJCOPY} -O binary -S ${MY_TARGET}.elf ${project_name_}.bin)

set(CMAKE_SIZE "${CROSS_COMPILE}size")
# 显示代码段大小
add_custom_command(TARGET ${MY_TARGET} POST_BUILD COMMAND ${CMAKE_SIZE} ${MY_TARGET}.elf)

 